<template>
  <div class="autoBillUpload">
    <el-form :model="ruleForm" :inline="true" size="mini" ref="ruleForm" label-width="100px" class="account">
      <el-form-item label="渠道类型">
        <el-select clearable v-model="ruleForm.exceltype" @change="changeexceltype">
          <el-option :label="item.names" v-for="(item, index) in type" :key="index" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报表渠道">
        <el-select clearable v-model="ruleForm.billBelong">
          <div v-if="this.changeexceltypeList == 'airline'">
            <el-option :label="item.airlinerShorter" v-for="(item, index) in airlinerList" :key="index"
              :value="item.id"></el-option>
          </div>
          <div v-if="this.changeexceltypeList == 'ticketingPlatform'">
            <el-option :label="item.name" v-for="(item, index) in reporttype" :key="index" :value="item.name">
            </el-option>
          </div>
          <div v-if="this.changeexceltypeList == 'paymentPlatform'">
            <el-option :label="item.names" v-for="(item, index) in channels" :key="index" :value="item.id"></el-option>
          </div>
        </el-select>
      </el-form-item>
      <el-form-item label="报表类型">
        <el-select v-model="ruleForm.reportType">
          <el-option :label="item.names" v-for="(item, index) in numDay" :key="index" :value="item.id" class="select">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件名字">
        <el-input v-model="ruleForm.newName"></el-input>
      </el-form-item>
      <el-form-item label="上传日期">
        <el-date-picker type="date" v-model="ruleForm.createDate" format="yyyy-MM-dd " placeholder="上传日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">查询</el-button>
        <el-button type="primary" @click="Upload">上传</el-button>
        <!-- <el-button >规则列表</el-button> -->
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :header-cell-style="{
        color: '#fff',
        background: '#6090EC',
        fontSize: '11px',
        fontWeight: 'normal',
      }" :data="auxiliary" size="mini" border stripe>
      <el-table-column label="报表渠道" prop="billBelong" width="120"></el-table-column>
      <!-- <el-table-column
        label="Excel条数"
        prop="billCount"
        width="90"
      ></el-table-column> -->
      <el-table-column label="文件名" prop="newName"></el-table-column>
      <el-table-column label="入账单条数" width="100" prop="billCountInput">
      </el-table-column>
      <el-table-column label="开始时间" prop="billStartDate"></el-table-column>
      <el-table-column label="结束时间" prop="billEndDate"></el-table-column>
      <el-table-column label="账单号" width="140" prop="billTimeNumber"></el-table-column>
      <el-table-column label="报表类型" width="80" prop="reportType"></el-table-column>
      <el-table-column label="操作人" width="80" prop="employeeName"></el-table-column>
      <el-table-column label="上传日期" width="80" prop="createDate"></el-table-column>
      <el-table-column label="账单状态" prop="billState" width="100px">
      </el-table-column>
      <el-table-column label="操作" width="300" prop="employee">
        <template slot-scope="scope">
          <el-popconfirm style="margin: 0 10px 0 0" title="确定对账吗？" @confirm="confirm">
            <el-button @click="cancelClick(scope.row)" slot="reference" size="mini" type="primary">对账</el-button>
          </el-popconfirm>
          <el-button type="primary" @click="dealwithorder(scope.row)" size="mini">查看</el-button>
          <el-button size="mini" type="primary" @click="handleDetail">对账结果</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
        :page-sizes="[10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotal">
      </el-pagination>
    </div>
    <!-- 查看原始账单 -->
    <el-dialog :visible.sync="cancellog" :close-on-click-modal="false" width="90%">
      <!-- <el-form
        :model="seeRefundList"
        :inline="true"
        size="mini"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="报表类型">
          <el-select clearable v-model="seeRefundList.reportType">
            <el-option
              :label="item.names"
              v-for="(item, index) in numDay"
              :key="index"
              :value="item.id"
              class="select"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报表渠道">
          <el-select clearable v-model="seeRefundList.billBelong">
            <el-option
              :label="item.name"
              v-for="(item, index) in reporttype"
              :key="index"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传日期">
          <el-date-picker
            type="date"
            v-model="seeRefundList.createDate"
            format="yyyy-MM-dd"
            placeholder="上传日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="票号">
          <el-input v-model="seeRefundList.aaa"></el-input>
        </el-form-item>
        <el-form-item label="账单记录号">
          <el-input v-model="seeRefundList.aaa"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">查询</el-button>
        </el-form-item>
      </el-form> -->
      <el-table :data="billingdetails" height="300" border size="mini">
        <el-table-column prop="recordNum" label="票号" width="120">
        </el-table-column>
        <el-table-column prop="channelType" label="账单渠道-支付类型" width="130">
        </el-table-column>
        <el-table-column prop="start" label="状态" width="60">
        </el-table-column>
        <el-table-column prop="orderNum" label="出票方订单号" width="170">
        </el-table-column>
        <el-table-column prop="faceValue" label="单张票面价" width="85">
        </el-table-column>
        <el-table-column prop="handlingFee" label="手续费" width="70">
        </el-table-column>
        <el-table-column prop="settlementPrice" label="单张票结算价" width="95">
        </el-table-column>
        <el-table-column prop="fuelInfrastructure" label="燃油+基建" width="100">
        </el-table-column>
        <el-table-column prop="ticketTime" label="出票时间"></el-table-column>
        <el-table-column prop="uploadDate" label="上传日期"></el-table-column>
        <el-table-column prop="generator" label="生成人" width="100">
        </el-table-column>
      </el-table>
      <el-button style="margin:10px 0 10px 48%" type="primary" @click="cancellog=false">取消</el-button>
      <!-- <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Total"
        >
        </el-pagination>
      </div> -->
    </el-dialog>
    <!-- 上传 -->
    <el-dialog :visible.sync="uploading" :close-on-click-modal="false" width="80%">
      <el-form :model="seeRefund" :rules="uploadingList" :inline="true" size="mini" ref="seeRefund" label-width="100px">
        <el-form-item label="类型" prop="exceltype">
          <el-select clearable v-model="seeRefund.exceltype" @change="changeexceltype">
            <el-option :label="item.names" v-for="(item, index) in type" :key="index" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报表渠道" prop="billBelong">
          <!--  切记 误删-->
          <!--  multiple -->
          <el-select multiple v-model="seeRefund.billBelong" @change="seeRefundbillBel" clearable>
            <div v-if="this.changeexceltypeList == 'airline'">
              <el-option :label="item.airlinerShorter" v-for="(item, index) in airlinerList" :key="index"
                :value="item.airlinerShorter"></el-option>
            </div>
            <div v-if="this.changeexceltypeList == 'ticketingPlatform'">

              <el-option :label="item.name" v-for="(item, index) in reporttype" :key="index" :value="item.name">
              </el-option>
            </div>
            <div v-if="this.changeexceltypeList == 'paymentPlatform'">
              <el-option :label="item.names" v-for="(item, index) in channels" :key="index" :value="item.id">
              </el-option>
            </div>
          </el-select>
        </el-form-item>
        <el-form-item label="报表类型" prop="reportType">
          <el-select clearable v-model="seeRefund.reportType">
            <el-option :label="item.names" v-for="(item, index) in numDay" :key="index" :value="item.id" class="select">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传日期" prop="billStartTime">
          <el-date-picker type="date" v-model="seeRefund.billStartTime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="上传日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="billEndTime">
          <el-date-picker type="date" value-format="yyyy-MM-dd HH:mm:ss" v-model="seeRefund.billEndTime"
            placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item
          label="上传文件"
          style="display: flex; margin: 0 20px 10px 0">
         <el-upload
            class="upload-demo"
            action="/file/fileMapping/fileUpload"
            :on-success='uploadedsuccessfully'
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item> -->
        <!--  切记 误删-->
        <!-- <el-form-item
          v-if="this.seeRefundbillBelongaaa != ''"
          label="上传文件"
          style="display: flex; margin: 0 20px 10px 0">
          <el-upload
            class="upload-demo"
            action="/file/fileMapping/fileUpload"
            :on-success="uploadedsuccessfully"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-change="handleLimit"
            multiple
            limit="1"
            :on-exceed="handleExceed">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item> -->

        <!-- <el-checkbox v-if="seeRefundbillBelongList.length>0" v-model="checked">是否选择支付平台</el-checkbox> -->

        <div style="margin:0 0 0 10px;color:red;">目前只有航司支持多选 出票平台和支付平台多选默认第一个</div>
        <el-form-item label="支付平台" v-if="seeRefundbillBelongList.length>0 && this.changeexceltypeList=='airline'"
          style="display: flex; margin: 0 20px 10px 0">
          <div style="display:flex;">
            <el-upload class="upload-demo" action="/file/fileMapping/fileUpload" :on-success="uploadedsuccessfullyaaaaa"
              :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" multiple :limit="1"
              :on-exceed="handleExceed">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <div style="margin:0 0 0 10px">不上传支付平台请忽略</div>
          </div>
        </el-form-item>
        <el-form-item v-for="(item,index) in seeRefundbillBelongList" :key="index" label="上传文件"
          style="display: flex; margin: 0 20px 10px 0">
          <div style="display:flex;">
            <el-upload class="upload-demo" action="/file/fileMapping/fileUpload" :on-success="uploadedsuccessfully"
              :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" multiple :limit="1"
              :on-exceed="handleExceed">
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
            <div style="margin:0 0 0 10px">{{item}}</div>
          </div>
        </el-form-item>
        <el-form-item style="margin: 0 0 0 45%">
          <el-button type="primary" @click="uploadbill('seeRefund')">保存</el-button>
          <el-button type="primary" @click="cancelUpload()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "autoBillUpload",
    components: {},
    data() {
      return {
        payFile: "",
        checked: false,
        billTimeNumber: "",
        seeRefundbillBelongaaa: "",
        seeRefundbillBelongList: [],
        // 查看分页总条数
        Total: 0,
        // 查看账单表格
        billingdetails: [],
        // 查看表单
        seeRefundList: {},
        // 上传成功回调
        uploadedsuccess: [],
        // 上传校验 
        uploadingList: {
          exceltype: [{
            required: true,
            message: "请选择类型",
            trigger: "change"
          }, ],
          billBelong: [{
            required: true,
            message: "请选择报表渠道",
            trigger: "change"
          }, ],
          reportType: [{
            required: true,
            message: "请选择报表类型",
            trigger: "change"
          }, ],
          billStartTime: [{
            required: true,
            message: "请选择开始时间",
            trigger: "change"
          }, ],
          billEndTime: [{
            required: true,
            message: "请选择结束时间",
            trigger: "change"
          }, ],
        },
        changeexceltypeList: "",
        seeRefund: {
          exceltype: "",
          billBelong: "",
          reportType: "",
          billStartTime: "",
          billEndTime: "",
        },
        pageSize: 10,
        currentPage: 1,
        pageTotal: 0,
        cancellog: false,
        uploading: false,
        value1: "",
        // 表格数据
        auxiliary: [{
          a:""
        }],
        tableData: [],
        type: [{
            id: 'airline',
            names: "航司",
          },
          {
            id: 'ticketingPlatform',
            names: "出票平台",
          },
          {
            id: 'paymentPlatform',
            names: "支付平台",
          },
        ],
        // 报表支付渠道
        channels: [{
            id: "",
            names: "全部",
          },
          {
            id: '1',
            names: "汇付天下",
          },
          // {
          //   id: '2',
          //   names: "支付宝",
          // },
          {
            id: '3',
            names: "易宝出票",
          },
          // {
          //   id: 4,
          //   names: "易生",
          // },
        ],
        // 报表类型
        reporttype: [{
            name: "汇付天下",
          },
          {
            name: "支付宝",
          },
          {
            name: "百拓",
          },
          {
            name: "航协出票",
          },
          {
            name: "财付通",
          },
          {
            name: "中航信平台",
          },
          {
            name: "易购",
          },
          {
            name: "其他",
          },
          {
            name: "易宝支付",
          },
          {
            name: "联拓天际",
          },

          {
            name: "金色世纪旅行社",
          },
          {
            name: "翔游天下",
          },
          {
            name: "北京517",
          },
          {
            name: "酷讯",
          },
          {
            name: "八千翼",
          },
          {
            name: "十九E",
          },

          {
            name: "北京纵横天下",
          },
          {
            name: "票盟",
          },
          {
            name: "得付通",
          },
          {
            name: "公务卡",
          },
          {
            name: "携程",
          },
          {
            name: "春秋",
          },
          {
            name: "易宝出票",
          },

          {
            name: "必去",
          },
          {
            name: "人工出票",
          },
          {
            name: "东航",
          },
          {
            name: "春秋柜台",
          },

          {
            name: "易生",
          },
          {
            name: "飞猪",
          },
          {
            name: "海航预存",
          },
          {
            name: "瑞丽",
          },

          {
            name: "深航预存",
          },
          {
            name: "华夏",
          },
          {
            name: "航班管家",
          },
        ],
        // 报表类型
        numDay: [{
            id: "sales",
            names: "销售",
          },
          {
            id: "refund",
            names: "退票",
          },
          {
            id: "salesAndRefunds",
            names: "销售和退票",
          },
        ],
        ruleForm: {
          exceltype: "",
          billBelong: "",
          reportType: "",
          newName: "",
          createDate: "",
        },
        // 航空公司
        airlinerList: [],
        // 上传的文件列表
        upLodeVoList: []
      };
    },
    methods: {
      // 对账确认回调
      confirm() {
        this.getreconciliation();
      },
      // 对账按钮
      cancelClick(row) {
        console.log(row);
        this.billTimeNumber = row.billRecordId;
      },
      getreconciliation() {
        localStorage.setItem("isToken", true);
        this.$axios
          .getreconciliation({
            billRecordId: this.billTimeNumber,
          })
          .then((res) => {
            console.log(res);
            if (res.code == "200") {
              this.$message({
                message: '对账成功，请前往结果页查看详情',
                type: "success",
              });
              this.getbillingInquiry()
            }
          });
      },
      seeRefundbillBelong(row) {
        console.log(row);
        this.seeRefundbillBelongaaa = row;
      },
      seeRefundbillBel(row) {
        console.log(row);
        this.seeRefundbillBelongList = row;
      },
      // 新增财务自动对账文件记录
      getautobillRecordadd() {
        if (this.changeexceltypeList != 'airline') {
          // let a =
          this.uploadList = [
            this.uploadedsuccess[0]
          ]
          // uploadedsuccess
        } else {
          this.uploadList = this.uploadedsuccess
        }
        localStorage.setItem("isToken", true);
        this.$axios
          .getautobillRecordadd({
            payFile: this.payFile,
            //渠道类型
            exceltype: this.seeRefund.exceltype,
            // 报表渠道
            billBelongList: this.seeRefund.billBelong,
            reportType: this.seeRefund.reportType,
            billStartTime: this.seeRefund.billStartTime,
            billEndTime: this.seeRefund.billEndTime,
            billEndTime: this.seeRefund.billEndTime,
            uploadList: this.uploadList
          })
          .then((res) => {
            console.log(res);
            if (res.code == "200") {
                this.$message({
                message: res.msg,
                type: "success",
              });
              this.uploading = false;
              this.getbillingInquiry()
              // this.$router.go(0)
            }
          });
      },
      // 上传保存按钮
      uploadbill(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid && this.uploadedsuccess) {
            this.getautobillRecordadd();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      uploadedsuccessfullyaaaaa(row) {
        console.log(row);
        this.payFile = row.data[0].name
      },
      // 文件上传成功
      uploadedsuccessfully(row) {

        console.log(row);
        let arr = row.data
        let obj = {}
        arr.forEach(item => {
          item.oldName = item.odlName
          delete item.odlName
          obj = item
          this.uploadedsuccess.push(obj)
        })
        // this.uploadedsuccess = row.data;
        console.log(this.uploadedsuccess);
      },
      //文件上传
      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePreview(file) {
        console.log(file);
      },
      handleExceed(files, fileList) {
        this.$message.warning(
          `抱歉，当前只能上传一个文件`
        );
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`);
      },
      dateChange(roq) {
        console.log(roq, this.ruleForm.starttime);
      },
      // 类型点击事件
      changeexceltype(row) {
        console.log(row);
        this.changeexceltypeList = row;
        console.log(this.changeexceltypeList);
        if (this.changeexceltypeList == 'airline' || this.changeexceltypeList == 'ticketingPlatform' || this
          .changeexceltypeList == 'paymentPlatform') {
          this.seeRefund.billBelong = '',
            this.seeRefundbillBelongList = []
        }
      },
      // 原始账单查询
      getbillingInquiry() {
        this.$axios
          .getbillingInquiry({
            exceltype: this.ruleForm.exceltype,
            billBelong: this.ruleForm.billBelong,
            reportType: this.ruleForm.reportType,
            newName: this.ruleForm.newName,
            createDate: this.ruleForm.createDate,
          })
          .then((res) => {
            console.log(res);
            if (res.code == "200") {
              this.auxiliary = res.data;
              this.pageTotal = res.total;
            }
          });
      },
      // 查看按钮
      dealwithorder(row) {
        console.log(row)
        this.$axios.getbillDetails({
          id: String(row.billRecordId)
        }).then((res) => {
          console.log(res)
          this.billingdetails = res.data
        })
        this.cancellog = true;
      },
      // 提交表单
      submitForm() {
        localStorage.setItem("pageSize", 10);
        localStorage.setItem("pageNum", 1);
        this.getbillingInquiry();
      },
      // 查看内表单
      viewBillDetails() {},
      // 重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      //分页
      handleSizeChange(value) {
        console.log("1", value);
        localStorage.setItem("pageSize", value);
        localStorage.setItem("pageNum", 1);
        this.getbillingInquiry();
        this.pageSize = value;
      },
      handleCurrentChange(value) {
        console.log("2", value);
        localStorage.setItem("pageNum", value);
        localStorage.setItem("pageSize", this.pageSize);
        this.getbillingInquiry();
        // orders = res.data;
      },
      // 查看对账结果
      handleClick(row) {
        this.cancellog = true;
        this.getorderFlightDetails(row.orderId);
      },
      // 查看对账结果
      handleDetail(row) {
        this.$router.push({
          name: "autoBillResult",
          params: {
            // id:row.refundmentId
          },
        });
        // this.getorderFlightDetails(row.orderId);
      },
      handleClose() {
        this.cancellog = false;
        this.uploading = false;
      },
      Upload() {
        this.payFile = '',
          this.seeRefund.exceltype = "",
          this.seeRefund.billBelong = "",
          this.seeRefund.reportType = "",
          this.seeRefund.billStartTime = "",
          this.seeRefund.billEndTime = ""
        this.uploadedsuccess = [],
          this.seeRefundbillBelongList = [],
          this.uploading = true;

      },
      // 取消上传点击事件那清空表单
      cancelUpload() {
        this.uploading = false
        this.seeRefund.exceltype = "",
          this.seeRefund.billBelong = "",
          this.seeRefund.reportType = "",
          this.seeRefund.billStartTime = "",
          this.seeRefund.billEndTime = ""
        this.seeRefundbillBelongList = []
      },
      // 航空公司
      getflightlist() {
        this.$axios.getflightlist({}).then((res) => {
          if (res.code == "200") {
            let list = JSON.parse(JSON.stringify(res.data));
            this.airlinerList = list.filter((item) => {
              if (item.airliner != null && item.airlinerShorter != null) {
                return item;
              } else {}
            });
            this.airlinerList.forEach((item) => {
              item.airlinerShorter = item.airlinerShorter + item.airlinerId;
              item.ids = String(item.id)
            });
            console.log(this.airlinerList);
          }
        });
      },
    },
    mounted() {
      this.getflightlist();
      this.getbillingInquiry();
    },
  };
</script>

<style scoped>
  .autoBillUpload {
    margin: 20px;
  }

  .account {
    margin: 30px 0 0 30px;
  }

  .account_table {
    width: 77%;
    margin: 30px 0 0 30px;
  }

  .block {
    margin: 10px 0 0 24px;
  }
</style>